/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.dsw2.vista;

import java.awt.event.ActionEvent;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import mx.itschapala.dsw2.biblioteca8a.bl.AutorBlLocal;
import mx.itschapala.dsw2.biblioteca8a.modelo.Autor;

/**
 *
 * @author RaygozaL
 */
@ManagedBean
@SessionScoped
public class AuthorBean {
    //---------ejb---------------
    @EJB
    private AutorBlLocal autorBl;
    
//----------------variables
    private  Autor author;
   private List<Autor> lista;
   private String accion;
   //------------contructor
   
    public AuthorBean() {
        author=new Autor();
    }
//-----------get y set
    public List<Autor> getLista() {
        lista=autorBl.getAll();
        return lista;
    }
    public Autor getAuthor() {
        return author;
    }

    public void setAuthor(Autor author) {
        this.author = author;
    }
    
    
  ///----------------acciones
    
    public String procesarAccion(){
        
       if (accion.equals("Nuevo")) {
            autorBl.register(author);

        } else {
            if (accion.equals("Editar")) {
                autorBl.modify(author);

            } else{
                if(accion.equals("Eliminar")){
                autorBl.delete(author);
                }
            }
            
        }
        accion = "";
        return "Autor";
    }

//    public void prepararNuevo(ActionEvent evt){
//        
//        author=new Autor();
//    accion="Nuevo";
//    }
    
    public String prepararNuevo(){
        
        author=new Autor();
        accion="Nuevo";
    return "AutorCrearEditar";
    }
    
    public void prepararEditar(javax.faces.event.ActionEvent evt){
       int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        System.out.println("el numero es " + index);
        author = lista.get(index);
        accion = "Editar";
        

    }
    public void prepararEliminar(javax.faces.event.ActionEvent evt) {

        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        System.out.println("el numero es " + index);
          author = lista.get(index);
        accion = "Eliminar";
      


    }
    
}
